<?php

namespace app\admin\controller;
use app\lib\enum\Status;

class Index extends BaseController
{
    //首页
    public function index(){
        //权限分配
        $this->assign("permissionData",$this->getPermissionData());
        $this->assign("adminInfo",session("adminInfo"));
        return view();
    }

    //权限禁止显示的页面
    public function forbidden() {
        return view();
    }

    //首页数据显示
    public function home(){
//        城市数量
//        机构数量
//        好文数量
//        好课数量

        $cityNum=count(model("PyCity")->select());
        $institutionNum=count(model("PyInstitution")->select());


        $goodArticleNum=count(model("CmsArticle")->where('art_category_id',"in",getGoodArticleSubCategoryIds())->select());
        $goodClassNum=count(model("CmsArticle")->where('art_category_id',"in",getGoodClassSubCategoryIds())->select());

        $backupNum=count(model("CmsDatabaseBackup")->select());
//
        $this->assign("cityNum",$cityNum);
        $this->assign("institutionNum",$institutionNum);
        $this->assign("goodArticleNum",$goodArticleNum);
        $this->assign("goodClassNum",$goodClassNum);
        $this->assign("backupNum",$backupNum);


        //好课待审核
        $goodClassNumWithNoCheck=count(model("CmsArticle")->where("art_ischeck","=",Status::off)
            ->where("art_category_id","in",getGoodClassSubCategoryIds())
            ->select());
        $this->assign("goodClassNumWithNoCheck",$goodClassNumWithNoCheck);

        //好文待审核
        $goodArticleNumWithOnCheck=count(model("CmsArticle")->where("art_ischeck","=",Status::off)
            ->where("art_category_id","in",getGoodArticleSubCategoryIds())
            ->select());
        $this->assign("goodArticleNumWithNoCheck",$goodArticleNumWithOnCheck);


        $this->assign("isInstitutionAdmin",isInstitutionAdmin());

        if($data=isInstitutionAdmin()){
            $ins_id=$data['ins_id'];
            $institutionGoodArticleNum=count(model('CmsArticle')->where("art_institution_id","=",$ins_id)
            ->where("art_category_id","in",getGoodArticleSubCategoryIds())->select());
            $institutionGoodClassNum=count(model('CmsArticle')->where("art_institution_id","=",$ins_id)
                ->where("art_category_id","in",getGoodArticleSubCategoryIds())->select());
            $this->assign("institutionGoodArticleNum",$institutionGoodArticleNum);
            $this->assign("institutionGoodClassNum",$institutionGoodClassNum);
        }


        return view();
    }

    public function getPermissionData() {
        $adminInfo = session("adminInfo");
        $role_list = $adminInfo['role_list'];
        $permissionData = [
            "personManage" => [
                "studentList" => $this->getItemPermissionStatus($role_list, "studentList"),
                "adminList" => $this->getItemPermissionStatus($role_list, "adminList"),
                "roleList" => $this->getItemPermissionStatus($role_list, "roleList"),
            ],
            "institutionManage"=>[
                "institutionList"=>$this->getItemPermissionStatus($role_list,"institutionList")
            ],
            "cityManage"=>[
                "cityList"=>$this->getItemPermissionStatus($role_list,"cityList")
            ],
            "articleManage"=>[
                "articleCategoryList"=>$this->getItemPermissionStatus($role_list,"articleCategoryList"),
                "goodArticleList"=>$this->getItemPermissionStatus($role_list,"goodArticleList"),
                "goodClassList"=>$this->getItemPermissionStatus($role_list,"goodClassList"),
            ],
            //招生信息菜单栏
//        "locationList"=>"地区管理",
//        "yearList"=>"年份管理",
//        "informationList"=>"信息管理",
//        "logList"=>"日志查看",
            "informationManage"=>[
                "locationList"=>$this->getItemPermissionStatus($role_list,"locationList"),
                "yearList"=>$this->getItemPermissionStatus($role_list,"yearList"),
                "informationList"=>$this->getItemPermissionStatus($role_list,"informationList"),
                "logList"=>$this->getItemPermissionStatus($role_list,"logList"),
            ],
            "recommendManage"=>[
                "carouselList"=>$this->getItemPermissionStatus($role_list,"carouselList"),
                "recommendList"=>$this->getItemPermissionStatus($role_list,"recommendList")
            ],
            "websiteManage"=>[
                "show"=>$this->getItemPermissionStatus($role_list,"show"),
                "backup"=>$this->getItemPermissionStatus($role_list,"backup")
            ]
        ];

        foreach($permissionData as &$value){
            $value['status']=$this->getTotalStatus($value);
        }

        return $permissionData;
    }


    public function getTotalStatus($arrayData){
        $status=Status::off;
        foreach($arrayData as $key=>$value){
            if($value!=Status::off){
                $status=Status::on;
                break;
            }
        }
        return $status;
    }



    public function getItemPermissionStatus($data, $itemName) {
        return in_array($itemName, $data) ? Status::on : Status::off;
    }

}
